import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Register from '@/views/Register'
import Fpw from '@/views/Fpw'
import Movie from '@/views/Movie'
import Myinfo from '@/views/Myinfo'
import Order from '@/views/Order'
import Orderdetail from '@/views/Order/detail'
import Detail from '@/views/Detail'
import Trailer from '@/views/Trailer'
import Plan from '@/views/Plan'
import seats from '@/views/seats'
import index from '@/views/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/index',
    name: 'index',
    component: index
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/test',
    name: 'test',
    component: () => import('@/views/test')
  },
  {
    path: '/fpw',
    name: 'Fpw',
    component: Fpw
  },
  {
    path: '/film',
    name: 'film',
    component: Movie
  },
  {
    name: 'toDetail',
    path: '/detail/:id', // 动态二级路由
    component: Detail
  },
  {
    path: '/preview',
    name: 'trailer',
    component: Trailer
  },
  {
    path: '/cinema',
    name: 'plan',
    component: Plan
  },
  {
    path: '/order',
    name: 'Order',
    component: Order,
  },
  {
    path: '/order/detail/:id',
    name: 'Orderdetail',
    component: Orderdetail
  },
  {
    path: '/myinfo',
    name: 'Myinfo',
    component: Myinfo
  },
  {
    path: '/seats',
    name: 'seats',
    component: seats
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由守卫监测除了注册页面和忘记密码页面外token为空时跳转到登录页
router.beforeEach((to, from, next) => {
  if (to.path === '/' || to.path === '/register' || to.path === '/fpw') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (token === null || token === '') {
      next('/')
    } else {
      next()
    }
  }
})

export default router
